package org.bonorsoft.modules.control.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.constant.UserConstants;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.common.system.dataquery.DataQueryGenerator;
import org.bonorsoft.common.system.vo.Ztree;
import org.bonorsoft.common.util.StringUtils;
import org.bonorsoft.modules.control.entity.Hazard;
import org.bonorsoft.modules.control.service.IHazardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/control/hazard")
public class HazardController extends BonorController<Hazard, IHazardService> {

    @Autowired
    private IHazardService hazardService;

    private String prefix = "control/hazard";

    @RequiresPermissions("control:hazard:view")
    @GetMapping()
    public String index() {
        return prefix + "/hazard";
    }


    @RequiresPermissions("control:hazard:list")
    @PostMapping("/list")
    @ResponseBody
    public List<Hazard> queryPageList(Hazard hazard, HttpServletRequest req) {
        QueryWrapper<Hazard> queryWrapper = DataQueryGenerator.initQueryWrapper(hazard, req);
        queryWrapper.orderByAsc("order_num");
        return hazardService.list(queryWrapper);
    }

    /**
     * 新增危险源点
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap) {

        Hazard hazard = null;
        if (0L != parentId) {
            hazard = hazardService.getById(parentId);
        } else {
            hazard = new Hazard().setHazardId(0L).setHazardName("根源点");
        }
        mmap.put("hazard", hazard);
        return prefix + "/add";
    }

    /**
     * 添加
     *
     * @param hazard
     * @return
     */
    @AutoLog(value = "危险源点-添加")
    @RequiresPermissions("control:hazard:add")
    @PostMapping(value = "/add")
    @ResponseBody
    public Result<?> add(@Validated Hazard hazard) {
        if (UserConstants.HAZARD_NAME_NOT_UNIQUE.equals(checkHazardNameUnique(hazard))) {
            return Result.error("新增危险源点'" + hazard.getHazardName() + "'失败，危险源名称已存在");
        }
        hazardService.save(hazard);
        return Result.ok("添加成功！");
    }

    /**
     * 校验危险源点名称
     */
    @PostMapping("/checkHazardNameUnique")
    @ResponseBody
    public String checkHazardNameUnique(Hazard hazard) {
        return hazardService.checkHazardNameUnique(hazard);
    }


    /**
     * 选择危险源点树
     */
    @GetMapping("/selectHazardTree/{hazardId}")
    public String selectDeptTree(@PathVariable("hazardId") Long hazardId, ModelMap mmap) {
        mmap.put("hazard", hazardService.getById(hazardId));
        return prefix + "/tree";
    }

    /**
     * 加载危险源点列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        return hazardService.selectHazardTree();
    }

    /**
     * 修改
     */
    @GetMapping("/edit/{hazardId}")
    public String edit(@PathVariable("hazardId") Long hazardId, ModelMap mmap) {
        mmap.put("hazard", hazardService.getById(hazardId));
        return prefix + "/edit";
    }

    /**
     * 编辑
     *
     * @param hazard
     * @return
     */
    @AutoLog(value = "危险源点-编辑")
    @RequiresPermissions("control:hazard:edit")
    @PostMapping(value = "/edit")
    @ResponseBody
    public Result<?> edit(@Validated Hazard hazard) {
        if (UserConstants.HAZARD_NAME_NOT_UNIQUE.equals(checkHazardNameUnique(hazard))) {
            return Result.error("修改危险源点'" + hazard.getHazardName() + "'失败，危险源点名称已存在");
        } else if (hazard.getParentId().equals(hazard.getHazardId())) {
            return Result.error("修改危险源点'" + hazard.getHazardName() + "'失败，上级危险源点不能是自己");
        }
        hazardService.updateHazard(hazard);
        return Result.ok("编辑成功!");
    }

    /**
     * 删除菜单
     */
    @AutoLog(value = "危险源点-删除", operateType = 4)
    @RequiresPermissions("control:hazard:remove")
    @DeleteMapping("/remove/{hazardId}")
    @ResponseBody
    public Result<?> remove(@PathVariable("hazardId") Long hazardId) {
        if (hazardService.selectCountHazardByParentId(hazardId) > 0) {
            return Result.error("存在子危险源点,不允许删除");
        }
        hazardService.removeById(hazardId);
        return Result.ok("删除成功");
    }
}
